////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 11   Version 10.0 (Current View)1.10        5/2/2011 11:50:17 AM   Jeff
//      Heigl      IDD#09523: mobile app infrastructure
// 10   Version 10.0 (Current View)1.9         2/14/2011 5:16:08 PM   Stephen
//      Kish    IDD#09460: Fixing transportLog warnings
// 9    Version 10.0 (Current View)1.8         2/9/2011 4:37:14 PM    Jeff
//      Heigl      IDD#09373: writing the selector alerts to the page xml and
//      itemXML
// 8    Version 10.0 (Current View)1.7         12/16/2010 9:47:29 AM  Stephen
//      Kish    IDD#7855: Moving pages to Abstract selector page design.
// 7    Version 10.0 (Current View)1.6         11/5/2010 1:05:18 PM   Stephen
//      Kish    IDD#6245: Refactoring changes
// 6    Version 10.0 (Current View)1.5         10/19/2010 2:26:10 PM  Stephen
//      Kish    IDD#6245: Refactoring HttpSessions.get and set Attribute to use
//      DAO classes
// 5    Version 10.0 (Current View)1.4         10/14/2009 3:10:23 PM  Jeff
//      Heigl      IDD#8193: infrastructure for gas constituents popup page
// 4    Version 10.0 (Current View)1.3         9/23/2009 5:03:23 PM   Jeff
//      Heigl      IDD#8162: transport log timings added, curve chartable and
//      director cache implemented
// 3    Version 10.0 (Current View)1.2         7/20/2009 12:26:33 PM  Jeff
//      Heigl      IDD#7911: page submit refactoring, comment cleanup
// 2    Version 10.0 (Current View)1.1         6/22/2009 12:05:20 PM  Jeff
//      Heigl      IDD#7911: compressor cos page and common xml impl
// 1    Version 10.0 (Current View)1.0         6/19/2009 4:57:12 PM   Jeff
//      Heigl      
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import javax.servlet.http.HttpServletRequest;

import com.intelliquip.constants.ClientDevice;
import com.intelliquip.constants.ProductType;
import com.intelliquip.core.widgets.KeyValuePair;
import com.intelliquip.hydraulicselection.UserPreferences;
import com.intelliquip.hydraulicselection.productline.ProductLine;
import com.intelliquip.hydraulicselection.productline.web.ProductLineWebImpl;
import com.intelliquip.util.ServletUtils;

public class DisplayController {

	public static PageFactory getPageFactory(HttpServletRequest request) {
		String screenUrl = getSelectedURL(request);
		int screenId = ScreenNames.getScreenIDByURL(screenUrl);
		PageFactory pageFactory = PageFactory.getByScreenId(screenId);
		return pageFactory;
	}

	public static String getSelectedURL(HttpServletRequest request) {
		return request.getParameter("selectedURL");
	}


	public static KeyValuePair<Integer, String> getScreenIdAndUrl(HttpServletRequest request) {
		String selectedURL = getSelectedURL(request);
		int currentScreen = getCurrentScreenId(selectedURL);
		String screenUrl = getScreenUrl(request, currentScreen);

		setSessionAttributes(request, selectedURL, currentScreen);

		return KeyValuePair.build(currentScreen, screenUrl);

	}

	private static int getCurrentScreenId(String selectedURL) {
		if(selectedURL != null) {
			return ScreenNames.getScreenIDByURL(selectedURL);
		} else {
			return ScreenNames.START_PAGE_SCREEN;
		}
	}

	private static String getScreenUrl(HttpServletRequest request, int screenId) {
		String url;
		if(isSupportedByDisplayServlet(screenId)) {
			url = getDisplayServletUrl(request);
		} else {
			url = "intelliquip/"+ScreenNames.getJSPByScreenID(screenId);
		}

		url += "?"+request.getQueryString();
		return url;
	}

	private static String getDisplayServletUrl(HttpServletRequest request) {
		/*String scheme_serverName_port = ServletUtils.getScheme_ServerName_Port(request);
		String contextPath = request.getContextPath();
		String url = scheme_serverName_port+contextPath+ScreenNames.DISPLAY_SERVLET_URL;

		return url;*/
		return ScreenNames.removeSlashFromURL(ScreenNames.DISPLAY_SERVLET_URL);
	}

	public static boolean isSupportedByDisplayServlet(int screenId) {
		PageFactory f = PageFactory.getByScreenId(screenId);
		return f != null;
	}

	public static boolean isSupportedByDisplayServlet(String screenUrl) {
		int screenID = ScreenNames.getScreenIDByURL(screenUrl);
		return isSupportedByDisplayServlet(screenID);
	}

	private static void setSessionAttributes(HttpServletRequest request, String selectedURL, int currentScreen) {
		SessionAttributeDAO_Selector dao = new HttpSessionAttributeDAO_Selector(request.getSession());
		dao.setSelectedURL(selectedURL);
		dao.setCurrentScreen(currentScreen);
	}

	public static void processPageSubmit(int screenId, NavigationFlags navFlags) {
		PageFactory pageFactory = PageFactory.getByScreenId(screenId);
		pageFactory.processPageSubmit(navFlags);
	}

	public static boolean isMobileDevice(HttpServletRequest request) {
		HttpSessionAttributeDAO_Selector dao = new HttpSessionAttributeDAO_Selector(request.getSession());
		UserPreferences userPreferences = dao.getUserPreferences();
		return userPreferences != null && ClientDevice.isMobile(userPreferences.getClientDevice());
	}

	public static boolean isPdPump(HttpServletRequest request) {
		UserPreferences userPreferences = new HttpSessionAttributeDAO_Selector(request.getSession()).getUserPreferences();
		String productlineIdSupplier = userPreferences.getSelectedProductLineIDSupplier();
		ProductLine productLine = new ProductLineWebImpl().getProductLineBySupplierID(productlineIdSupplier, userPreferences.getOrgID());
		boolean isPdPump = productLine != null && productLine.getProductType() == ProductType.PD;
		return isPdPump;
	}





}
